package cn.jx.asc.controller;

import cn.jx.asc.common.constant.netconst;
import cn.jx.asc.common.enums.ResultStatus;
import cn.jx.asc.common.response.Result;
import cn.jx.asc.common.response.ResultUtils;
import cn.jx.asc.dao.mapper.PipeStatusMapper;
import cn.jx.asc.parseupload.OkHttpUtil2;
import cn.jx.asc.pojo.bo.SwitchBO;
import cn.jx.asc.pojo.entity.PipeStatus;
import cn.jx.asc.pojo.entity.Threshold;
import cn.jx.asc.service.PipeService;
import cn.jx.asc.service.PipeStatusService;
import cn.jx.asc.service.ThresholdService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 控制中心
 *
 * @author lhg
 */


@Api(value = "控制中心", tags = {"控制中心的相关接口"})
@RestController
@RequestMapping("/console")
public class ConsoleController {

    @Autowired
    private ThresholdService thresholdService;

    @Autowired
    private PipeService pipeService;

    @Autowired
    private PipeStatusService pipeStatusService;

    @Autowired
    private PipeStatusMapper pipeStatusMapper;



    @RequiresRoles("ADMIN")
    @ApiOperation(value = "查询阈值记录", notes = "查询阈值记录", httpMethod = "GET")
    @GetMapping("/queryThreshold")
    public Result queryThreshold(
            @ApiParam(name = "id",value = "阈值模板id")
            Integer id){
        if (id == null){
            id=1;
        }

        Threshold th = thresholdService.queryThreshold(id);
        th.setCreatedTime(null);
        th.setUpdatedTime(null);
        return ResultUtils.resultWithData(ResultStatus.SUCCESS,th);
    }

    @ApiOperation(value = "修改液高阈值", notes = "修改液高阈值", httpMethod = "POST")
    @PostMapping("/updateThreshold")
    public Result updateThreshold(
            @ApiParam(name = "threshold",value = "阈值对象")
            @RequestBody Threshold threshold) {
        if (threshold.getId() == null) {
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }
        String maxLiquidLevel = threshold.getMaxLiquidLevel();
        String minLiquidLevel = threshold.getMinLiquidLevel();
        String maxAngle = threshold.getMaxAngle();
        String maxSoilHumidity = threshold.getMaxSoilHumidity();
        OkHttpUtil2 okHttpUtil2 = new OkHttpUtil2();
        okHttpUtil2.issue("A,7," + maxAngle + "," + maxSoilHumidity);
        //等几秒等他回复
        try {
            Thread.sleep(1000 * 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        okHttpUtil2.issue("A,8,"+maxLiquidLevel+","+minLiquidLevel);
        try {
            Thread.sleep(1000 * 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (netconst.WARING_THRESHOLD != netconst.NO&&netconst.HEIGHT_THRESHOLD != netconst.NO) {
            boolean b = thresholdService.updateThreshold(threshold);
            if (b){
                return ResultUtils.resultWithoutData(ResultStatus.MODIFIED);
            }
        }
        return ResultUtils.resultWithoutData(ResultStatus.FAILED);
    }

    @ApiOperation(value = "远程控制开关", notes = "远程控制开关", httpMethod = "POST")
    @PostMapping("/modifyValveStatus")
    public Result modifyValveStatus(@RequestBody SwitchBO switchBO){
        final Integer pipeId = switchBO.getPipeId();
        final Integer switchStatus = switchBO.getSwitchStatus();

        //TODO 下发命令至onenet
        OkHttpUtil2 okHttpUtil2 = new OkHttpUtil2();
        final PipeStatus status = pipeStatusService.getById(pipeId);
        final Integer consoleStatus = status.getConsoleStatus();
        System.out.println(consoleStatus);
        System.out.println(consoleStatus == 1);
        if (consoleStatus == 1){
            okHttpUtil2.issue("A,A,"+pipeId+","+switchStatus);
            return ResultUtils.resultWithoutData(ResultStatus.SUCCESS);
        }
        return ResultUtils.resultWithoutData(ResultStatus.FAILED);

    }

    @ApiOperation(value = "更改控制状态", notes = "更改控制状态", httpMethod = "POST")
    @PostMapping("/modifyConsoleStatus")
    public Result modifyConsoleStatus(@RequestBody SwitchBO switchBO){
        final Integer controlState = switchBO.getControlState();

        System.out.println("hhhh"+switchBO);
        OkHttpUtil2 okHttpUtil2 = new OkHttpUtil2();
        okHttpUtil2.issue("A,3,"+controlState);
        //延时5秒等待对方响应，并解析数据
        try {
            Thread.sleep(1000*10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (netconst.CONSELE==netconst.NO){
            return ResultUtils.resultWithoutData(ResultStatus.PIPE_REFUSE);
        }
        return ResultUtils.resultWithoutData(ResultStatus.SUCCESS);

    }
    @ApiOperation(value = "设置注液管高度", notes = "设置注液管高度", httpMethod = "POST")
    @PostMapping("/modifyPipeHeight")
    public Result modifyPipeHeight(
            @ApiParam(name = "id",value = "注液管id")
                    Integer id,
            @ApiParam(name = "height",value = "阈值状态",required = true)
                    Integer height ){
        if (id == null){
            id=1;
        }
        final QueryWrapper<PipeStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pipe_id",id);
        final PipeStatus one = pipeStatusService.getOne(queryWrapper);
        one.setHeight(height);
        one.setCreatedTime(null);
        final boolean b = pipeStatusService.updateById(one);
        if (b){
            OkHttpUtil2 okHttpUtil2 = new OkHttpUtil2();
            okHttpUtil2.issue("A,B,"+height);
            return ResultUtils.resultWithoutData(ResultStatus.MODIFIED);
        }
        return ResultUtils.resultWithoutData(ResultStatus.FAILED);
    }

}
